package org.msfj.common.util;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 断言类
 * @Author: like
 * @Date: 2017-10-20 15:49
 * @Version: 1.0
 * @Created in idea by autoCode
 */
public abstract class Assert{

    /**  */
    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new IllegalArgumentException(message);
        }
    }

    /**  */
    public static void notEquals(Object object, Object object1, String message) {
        if (!object.equals(object1)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**  */
    public static void isNull(Object object, String message) {
        if (object != null) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * (key_IS_NULL)
     */
    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new IllegalArgumentException(message);
        }
    }

    /**  */
    public static void hasLength(String text, String message) {
        if (StrUtil.isEmpty(text)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**  */
    public static void hasText(String text, String message) {
        if (StrUtil.isBlank(text)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**  */
    public static void doesNotContain(String textToSearch, String substring, String message) {
        if (StrUtil.isNotBlank(textToSearch) && StrUtil.isNotBlank(substring)
                && textToSearch.contains(substring)) {
            throw new IllegalArgumentException(message);
        }
    }

    /** */
    public static void notEmpty(Object[] array, String message) {
        if (ObjectUtils.isEmpty(array)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**  */
    public static void noNullElements(Object[] array, String message) {
        if (array != null) {
            for (Object element : array) {
                if (element == null) {
                    throw new IllegalArgumentException(message);
                }
            }
        }
    }

    /**  */
    public static void notEmpty(Collection<?> collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**  */
    public static void notEmpty(Map<?, ?> map, String message) {
        if (CollectionUtils.isEmpty(map)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**  */
    public static void isInstanceOf(Class<?> type, Object obj, String message) {
        notNull(type, message);
        if (!type.isInstance(obj)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**  */
    public static void isAssignable(Class<?> superType, Class<?> subType, String message) {
        notNull(superType, message);
        if (subType == null || !superType.isAssignableFrom(subType)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 空字符或NULL
     */
    public static void isBlank(String text, String message) {
        if (StrUtil.isNotBlank(text)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 非空字符串(key_IS_NULL)
     */
    public static void isNotBlank(String text, String message) {
        if (StrUtil.isBlank(text)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 允许最小值
     */
    public static void min(Integer value, Integer min, String message) {
        notNull(value, message);
        if (value < min) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 允许最大值
     */
    public static void max(Integer value, Integer max, String message) {
        notNull(value, message);
        if (value > max) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 允许值范围
     */
    public static void range(Integer value, Integer min, Integer max, String message) {
        min(value, min, message);
        max(value, max, message);
    }

    /**
     * 允许最小值
     */
    public static void min(Float value, Float min, String message) {
        notNull(value, message);
        if (value < min) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 允许最大值
     */
    public static void max(Float value, Float max, String message) {
        notNull(value, message);
        if (value > max) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 允许值范围
     */
    public static void range(Float value, Float min, Float max, String message) {
        min(value, min, message);
        max(value, max, message);
    }

    /**
     * 允许最小值
     */
    public static void min(Double value, Double min, String message) {
        notNull(value, message);
        if (value < min) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 允许最大值
     */
    public static void max(Double value, Double max, String message) {
        notNull(value, message);
        if (value > max) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 允许值范围
     */
    public static void range(Double value, Double min, Double max, String message) {
        min(value, min, message);
        max(value, max, message);
    }

    /**
     * 字符长度(key_LENGTH)
     */
    public static void length(String text, Integer min, Integer max, String message) {
        notNull(text, message);
        if (min != null && text.length() < min) {
            throw new IllegalArgumentException(message);
        }
        if (max != null && text.length() > max) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 未来某一天
     */
    public static void future(Date date, String message) {
        if (date != null && date.compareTo(new Date()) <= 0) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 身份证
     */
    public static void idCard(String text, String message) {
        if (!IdcardUtil.isValidCard(text)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 邮箱
     */
    public static void email(String text, String message) {
        String regex = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        pattern(text, regex, true, message);
    }

    /**
     * 手机号
     */
    public static void mobile(String text, String message) {
        String regex = "((^(13|15|17|18)[0-9]{9}$)|(^0[1,2]{1}\\d{1}-?\\d{8}$)|(^0[3-9] {1}\\d{2}-?\\d{7,8}$)|(^0[1,2]{1}\\d{1}-?\\d{8}-(\\d{1,4})$)|(^0[3-9]{1}\\d{2}-? \\d{7,8}-(\\d{1,4})$))";
        pattern(text, regex, true,message);
    }

    /**
     * 正则表达式
     */
    public static void pattern(String text, String regex, boolean flag, String message) {
        boolean result = false;
        try {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(text);
            result = matcher.matches();
        } catch (Exception e) {
            result = false;
        }
        if (result != flag) {
            throw new IllegalArgumentException(message);
        }
    }
}
